package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "user/login";
    }

    // 注册页面
    @GetMapping("/register")
    public String registerPage() {
        return "user/register";
    }

    // 修改密码页面
    @GetMapping("/password")
    public String passwordPage(HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/login";
        }
        return "user/password";
    }

    // 登录处理
    @PostMapping("/login")
    public String login(@RequestParam String username,
                       @RequestParam String password,
                       HttpSession session,
                       Model model) {
        User user = userService.login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/product/search";
        }
        model.addAttribute("error", "用户名或密码错误");
        return "user/login";
    }

    // 注册处理
    @PostMapping("/register")
    public String register(@RequestParam String username,
                         @RequestParam String password,
                         @RequestParam String email,
                         Model model) {
        if (userService.isUsernameExists(username)) {
            model.addAttribute("error", "用户名已存在");
            return "user/register";
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);

        if (userService.register(user)) {
            return "redirect:/user/login?registered=true";
        } else {
            model.addAttribute("error", "注册失败，请重试");
            return "user/register";
        }
    }

    // 修改密码处理
    @PostMapping("/password")
    public String updatePassword(@RequestParam String oldPassword,
                               @RequestParam String newPassword,
                               HttpSession session,
                               Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        if (userService.updatePassword(user.getId(), oldPassword, newPassword)) {
            session.removeAttribute("user"); // 密码修改成功后退出登录
            return "redirect:/user/login?passwordUpdated=true";
        } else {
            model.addAttribute("error", "原密码错误或修改失败");
            return "user/password";
        }
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/user/login?logout=true";
    }
} 